package com.example.zhoutao.puzzle.Data;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.util.Log;
import java.io.ByteArrayOutputStream;


/**
 * @Author ZT on 2019.4.22
 * @description 历史纪录管理
 */

public class HistoryDataManager {

    private static final String TAG = "HistoryDataManager";
    private static final String DB_NAME = "user_history";
    private static final String TABLE_NAME = "history";
    public static final String ID = "_id";
    public static final String STEP = "history_step";
    public static final String DATE = "history_date";
    public static final String TIME = "history_time";
    public static final String DIFF = "history_diff";
    public static final String MODE = "history_mode";
    public static final String NAME = "history_name";
    public static final String PHOTO = "history_photo";
    private String step,date,time,diff,mode,name;
    private static final int DB_VERSION = 2;
    private Context mContext = null;

    //创建历史纪录book表
    private static final String DB_CREATE="CREATE TABLE " + TABLE_NAME + " ("
            + ID + " integer primary key," + STEP + " varchar," + DATE + " varchar,"
            + TIME +" varchar," + DIFF + " varchar," + MODE + " varchar," + NAME + " varchar,"+ PHOTO + " BLOB" + ");";

    private SQLiteDatabase sqLiteDatabase=null;
    private HistoryDBHelper historyDBHelper=null;



    //继承SQLiteOpenHelper
    private static class HistoryDBHelper extends SQLiteOpenHelper{

        HistoryDBHelper(Context context){
            super(context,DB_NAME,null,DB_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            Log.i(TAG,"db.getVersion()="+db.getVersion());
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME + ";");
            db.execSQL(DB_CREATE);
            Log.i(TAG, "db.execSQL(DB_CREATE)");
            Log.e(TAG, DB_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

            Log.i(TAG, "HistoryDBHelper onUpgrade");
            onCreate(db);
        }
    }

    public HistoryDataManager(Context context){
        mContext=context;
        Log.i(TAG,"HistoryDataManager construction!");
    }

    //打开数据库
    public void openDataBase() throws SQLException{
        historyDBHelper=new HistoryDBHelper(mContext);
        sqLiteDatabase=historyDBHelper.getWritableDatabase();
    }

    //关闭数据库
    public void closeDataBase() throws SQLException{
        historyDBHelper.close();
    }

    //添加数据
    public long insertHistoryData(HistoryData historyData){
        step = historyData.getHistoryStep();
        date = historyData.getHistoryDate();
        time = historyData.getHistoryTime();
        diff = historyData.getHistoryDiff();
        mode = historyData.getHistoryMode();
        name = historyData.getHistoryName();
        byte[] photo=SaveBitmap(historyData.getHistoryPhoto());
        ContentValues values = new ContentValues();
        values.put(STEP,step);
        values.put(DATE,date);
        values.put(TIME,time);
        values.put(DIFF,diff);
        values.put(MODE,mode);
        values.put(NAME,name);
        values.put(PHOTO,photo);
        Log.i(TAG, "数据库插入"+step+date+time+diff+mode+name+"照片："+photo);
        return sqLiteDatabase.insert(TABLE_NAME,ID,values);
    }

    //获取用户历史纪录
    public Cursor fetchUserHistory(String username,String diff){
        Cursor cursor=sqLiteDatabase.query(TABLE_NAME,null,NAME +
                "='"+username+"'"+" and "+DIFF+"='"+diff+"'",null,null,null," cast(history_time as '9999')"); //String类型转化进行排序，若只用TIME，只是对首字符进行排序
        if (cursor == null) {
            Log.i(TAG, "当前数据库无数据或报错");

        }else {

        }
        return cursor;
    }

    /**
     * Bitmap.CompressFormat.JPEG 和 Bitmap.CompressFormat.PNG
     * JPEG 与 PNG 的是区别在于 JPEG是有损数据图像，PNG使用从LZ77派生的无损数据压缩算法。
     * 这里建议使用PNG格式保存
     * 100 表示的是质量为100%。当然，也可以改变成你所需要的百分比质量。
     * os 是定义的字节输出流
     *
     * .compress() 方法是将Bitmap压缩成指定格式和质量的输出流
     */

    public byte[] SaveBitmap(Bitmap bitmap){
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG,100,os);
        return os.toByteArray();
    }

}
